package com.tianxin.workbao.workBao.fragment;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.CoalStatistidAdapter;
import com.tianxin.workbao.workBao.bean.Inputbill;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.dataProvide.InputProvide;
import com.tianxin.workbao.workBao.dataProvide.OutputProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hacker on 2017/7/18.
 */

public class CoalStatisticFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, UpdateListViewListener {

    private CoalStatistidAdapter mAdapter;
    private Integer dataType;
    private Context mContext;
    private List<Inputbill> inputList = new ArrayList<>();
    private List<OutputBill> outputBillList = new ArrayList<>();
    private InputProvide inputProvide;
    private OutputProvide outputProvide;

    public static CoalStatisticFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt("type", type);
        CoalStatisticFragment fragment = new CoalStatisticFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void CoalStatisticFragment(Context context) {
        this.mContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        this.dataType = bundle.getInt("type");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void initRootView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.common_content, container, false);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.recycler_color1, R.color.recycler_color2,
                R.color.recycler_color3, R.color.recycler_color4);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        //设置布局管理器
        mLayoutManager = new GridLayoutManager(getActivity(), spanCount);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return mAdapter.getItemColumnSpan(position);
            }
        });
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        if (dataType == 0 || dataType == 2) {
            mAdapter = new CoalStatistidAdapter(inputList, getContext(), dataType);
        } else {
            mAdapter = new CoalStatistidAdapter(outputBillList, getContext(), dataType);
        }
        mRecyclerView.setAdapter(mAdapter);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void initEvents() {
    }

    @Override
    public void search(String query) {
    }

    @Override
    public void initData(boolean isSavedNull) {
        onRefresh();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public void onRefresh() {
        if (mSwipeRefreshLayout.isRefreshing() == false) {
            mSwipeRefreshLayout.setRefreshing(true);
            if (dataType == 0 || dataType == 2) {
                getInputProvide().getStatistic(dataType);
            } else {
                getOutputProvide().getStatistic(dataType);
            }
        }
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void showProgress() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefreshData(Object result) {
        if (dataType == 0 || dataType == 2) {
            inputList.clear();
            if (result instanceof List) {
                inputList.addAll((List<Inputbill>) result);
            }
        } else {
            outputBillList.clear();
            if (result instanceof List) {
                outputBillList.addAll((List<OutputBill>) result);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    public InputProvide getInputProvide() {
        if (inputProvide == null) {
            inputProvide = new InputProvide(getContext(), this);
        }
        return inputProvide;
    }

    public OutputProvide getOutputProvide() {
        if (outputProvide == null) {
            outputProvide = new OutputProvide(getContext(), this);
        }
        return outputProvide;
    }
}
